﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Messaging;
using System.Threading;

namespace ServidorDeGestionDeClientes
{
    public class PayQueueManager
    {
        string LocalQueueForPayName = ConfigurationManager.AppSettings["LocalQueueForPay"];
        string LocalServerName = ConfigurationManager.AppSettings["LocalServer"];

        public void ReceiveMessage()
        {
            while (true)
            {
                string queueName = @LocalServerName + "\\Private$\\" + LocalQueueForPayName;
                if (MessageQueue.Exists(queueName))
                {
                    using (MessageQueue colaLocal = new MessageQueue(queueName))
                    {
                        colaLocal.Formatter = new BinaryMessageFormatter();
                        Message msg = colaLocal.Receive();
                        Thread HiloAccion = new Thread(AccionMensaje);
                        HiloAccion.Start(msg);
                    }
                }
                else
                {
                    using (MessageQueue colaLocal = MessageQueue.Create(queueName))
                    {
                        colaLocal.Formatter = new BinaryMessageFormatter();
                        Message msg = colaLocal.Receive();
                        Thread HiloAccion = new Thread(AccionMensaje);
                        HiloAccion.Start(msg);
                    }
                }
            }
        }

        private void AccionMensaje(object o)
        {
            Message Mensaje = (Message)o;
            string Accion = Mensaje.Label;
            string Datos = Mensaje.Body.ToString();
            ClientServer.AccionMensajes(Accion, Datos);
        }
    }
}
